/*
 * Copyright 2011 Clean GWT
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package com.cleangwt.validation.client;

/**
 * The utility class for common checking.
 * 
 * @author Jess
 * @version 1.0
 * @since 1.0
 */
public class Checks {

  /**
   * Checks whether the values contain any null or empty value.
   * 
   * @param values the values
   * @return true if the values contains any null or empty value.
   */
  public static boolean hasEmptyValue(Object... values) {
    for (Object value : values) {
      if (isEmptyValue(value)) {
        return true;
      }
    }
    return false;

  }

  /**
   * Checks whether the values all are empty or null.
   * 
   * @param values the values
   * @return true if all the values all are empty or null.
   */
  public static boolean isAllEmptyValue(Object[] values) {
    for (Object value : values) {
      if (!isEmptyValue(value)) {
        return false;
      }
    }
    return true;
  }

  /**
   * Check whether then value is null or empty.
   * 
   * @param value the value
   * @return true if then value is null or empty.
   */
  public static boolean isEmpty(String value) {
    return value == null || value.isEmpty();
  }

  /**
   * Checks whether the value is null or string-empty.
   * 
   * @param value the value
   * @return true if the value is null or string-empty.
   */
  public static boolean isEmptyValue(Object value) {
    if (value == null) {
      return true;
    }
    if (value instanceof String) {
      return ((String) value).isEmpty();
    }
    return false;
  }

  /**
   * Checks whether value1 equals to value2 or both null.
   * 
   * @param value1 the value1
   * @param value2 the value2
   * @return true if value1 equals to value2 or both null.
   */
  public static boolean isSame(Object value1, Object value2) {
    return (value1 == null) ? (value2 == null) : value1.equals(value2);
  }

  /**
   * Compares two values and ignores number type, that means double value 1.0 is the same with int
   * value 1.
   * 
   * @param value1 the value1
   * @param value2 the value2
   * @return 1 for greater than, 0 for equals , -1 for less than
   */
  @SuppressWarnings({"unchecked", "rawtypes"})
  public static <T extends Comparable> int compare(T value1, T value2) {
    if (value1 == null) {
      if (value2 == null) {
        return 0;
      } else {
        return -1;
      }
    } else if (value2 == null) {
      return 1;
    } else {
      if (value1 instanceof Number) {
        Double v1 = ((Number) value1).doubleValue();
        Double v2 = ((Number) value2).doubleValue();
        return v1.compareTo(v2);
      } else {
        return value1.compareTo(value2);
      }
    }
  }

  // public static int compare(Comparable<?> value1, String value2) {
  // if (value1 == null) {
  // if (value2 == null) {
  // return 0;
  // } else {
  // return -1;
  // }
  // } else if (value2 == null) {
  // return 1;
  // } else {
  // if (value1 instanceof Number) {
  // Double v1 = ((Number) value1).doubleValue();
  // return v1.compareTo(Double.valueOf(value2));
  // } else if(value1 instanceof String){
  // return ((String)value1).compareTo(value2);
  // } else {
  // throw new IllegalArgumentException("Unsupported comparable type : " +
  // value1.getClass().getName());
  // }
  // }
  // }

}
